package org.maven.dependencybrowser.implementations.zip;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.maven.dependencybrowser.implementations.Workspace;
import org.maven.dependencybrowser.implementations.WorkspaceLoader;
import org.maven.dependencybrowser.model.ArtifactInitialisationException;
import org.maven.dependencybrowser.model.RootPom;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class ZipFileWorkspaceLoader implements WorkspaceLoader
{

    private Workspace workspace;
    private JFrame frame;

    public void loadPom()
    {
        try
        {
            String selectedPom = open();
            if (selectedPom != null)
            {
                zipWorkspace().loadRootPom(selectedPom);
            }
        }
        catch (ZipException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (ArtifactInitialisationException e)
        {
            e.printStackTrace();
        }
    }

    public RootPom getRoot()
    {
        return workspace.getRoot();
    }

    public void reloadPom() throws ArtifactInitialisationException
    {
        workspace.reloadPomFile();
    }

    private ZipFileWorkspace zipWorkspace()
    {
        if (workspace instanceof ZipFileWorkspace)
        {
            return (ZipFileWorkspace) workspace;
        }
        throw new RuntimeException("Expected workspace of type " + ZipFileWorkspace.class.getName() + " not " + workspace.getClass().getName());
    }

    private String open() throws ZipException, IOException
    {
        final JDialog d = new JDialog(frame, true);
        final StringBuffer selectedBuffer = new StringBuffer();
        d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        d.getContentPane().setLayout(new BoxLayout(d.getContentPane(), BoxLayout.PAGE_AXIS));

        List poms = new ArrayList();
        ZipFile zipFile = zipWorkspace().getZipFile();
        Enumeration zipFileEntries = zipFile.entries();
        while (zipFileEntries.hasMoreElements())
        {
            ZipEntry zipEntry = (ZipEntry) zipFileEntries.nextElement();
            if (zipEntry.getName().startsWith(ZipFileWorkspace.PREFIX_WORKSPACE))
            {
                poms.add(zipEntry.getName().substring(ZipFileWorkspace.PREFIX_WORKSPACE.length()));
            }
        }
        JList list = new JList(poms.toArray());
        list.addMouseListener(new MouseAdapter()
        {

            public void mouseClicked(MouseEvent e)
            {
                if (e.getClickCount() > 1)
                {
                    JList l = (JList) e.getSource();
                    selectedBuffer.append((String) l.getSelectedValue());
                    d.dispose();
                }
            }

        });

        d.getContentPane().add(new JScrollPane(list));
        d.pack();
        d.setVisible(true);
        String selectedPom = selectedBuffer.toString().trim();
        if (selectedPom.length() == 0)
        {
            return null;
        }
        else
        {
            return selectedPom;
        }
    }

}
